<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/murmur/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/murmur/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/murmur/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/murmur/images/logo.svg" color="#222">

<link rel="stylesheet" href="/murmur/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.css">
  <script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.js"></script>

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gitee.com","root":"/murmur/","scheme":"Gemini","version":"8.0.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"disqus","storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}};
  </script>

  <meta name="description" content="了解Java程序结构，package语句以及import语句；掌握Java主要语句的格式；掌握异常处理的概念以及try&#x2F;catch&#x2F;finally&#x2F;thow&#x2F;throws的使用">
<meta property="og:type" content="article">
<meta property="og:title" content="计应本的自考笔记(3)-Java程序的流程控制">
<meta property="og:url" content="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(3)-%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/index.html">
<meta property="og:site_name" content="汇话板">
<meta property="og:description" content="了解Java程序结构，package语句以及import语句；掌握Java主要语句的格式；掌握异常处理的概念以及try&#x2F;catch&#x2F;finally&#x2F;thow&#x2F;throws的使用">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-02-26T15:58:12.000Z">
<meta property="article:modified_time" content="2022-03-03T15:05:37.023Z">
<meta property="article:author" content="William Leung">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(3)-%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>计应本的自考笔记(3)-Java程序的流程控制 | 汇话板</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?a49081c570a782a242cdf6e19e1a584d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/murmur/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">汇话板</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">设定目标，放慢步调，用温和的力量自我改革</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/murmur/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/murmur/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <section class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Java%E7%A8%8B%E5%BA%8F%E7%9A%84%E7%BB%93%E6%9E%84"><span class="nav-number">1.</span> <span class="nav-text">Java程序的结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6"><span class="nav-number">2.</span> <span class="nav-text">流程控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1%E3%80%81%E8%B5%8B%E5%80%BC%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.1.</span> <span class="nav-text">1、赋值语句</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2%E3%80%81%E5%88%86%E6%94%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.2.</span> <span class="nav-text">2、分支语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#if%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.2.1.</span> <span class="nav-text">if语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#switch%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.2.2.</span> <span class="nav-text">switch语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3%E3%80%81%E5%BE%AA%E7%8E%AF%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.3.</span> <span class="nav-text">3、循环语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#for%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.3.1.</span> <span class="nav-text">for语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#while%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.3.2.</span> <span class="nav-text">while语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#do-while%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.3.3.</span> <span class="nav-text">do-while语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4%E3%80%81%E8%B7%B3%E8%BD%AC%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.</span> <span class="nav-text">4、跳转语句</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#break%E8%AF%AD%E5%8F%A5%E5%92%8Ccontainer%E8%AF%AD%E5%8F%A5"><span class="nav-number">2.4.1.</span> <span class="nav-text">break语句和container语句</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BE%93%E5%85%A5-%E8%BE%93%E5%87%BA"><span class="nav-number">3.</span> <span class="nav-text">输入&#x2F;输出</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Scanner%E7%B1%BB"><span class="nav-number">3.1.</span> <span class="nav-text">Scanner类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#NumberFormat%E7%B1%BB%E5%92%8CDecimalFormat%E7%B1%BB"><span class="nav-number">3.2.</span> <span class="nav-text">NumberFormat类和DecimalFormat类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%84%E7%90%86%E5%BC%82%E5%B8%B8"><span class="nav-number">4.</span> <span class="nav-text">处理异常</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E5%8F%8A%E5%A4%84%E7%90%86%E6%9C%BA%E5%88%B6"><span class="nav-number">4.1.</span> <span class="nav-text">异常及处理机制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E5%88%86%E7%B1%BB"><span class="nav-number">4.2.</span> <span class="nav-text">异常分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="nav-number">4.3.</span> <span class="nav-text">异常处理</span></a></li></ol></li></ol></div>
        </section>
        <!--/noindex-->

        <section class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">William Leung</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/murmur/archives">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/murmur/categories/">
          
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/murmur/tags/">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </section>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">
      

      

  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(3)-%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/murmur/images/avatar.gif">
      <meta itemprop="name" content="William Leung">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="汇话板">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          计应本的自考笔记(3)-Java程序的流程控制
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-02-26 23:58:12" itemprop="dateCreated datePublished" datetime="2022-02-26T23:58:12+08:00">2022-02-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-03-03 23:05:37" itemprop="dateModified" datetime="2022-03-03T23:05:37+08:00">2022-03-03</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/murmur/categories/%E8%AE%A1%E5%BA%94%E6%9C%AC%E7%9A%84%E8%87%AA%E8%80%83%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">计应本的自考笔记</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>4 分钟</span>
    </span>
</div>

            <div class="post-description">了解Java程序结构，package语句以及import语句；掌握Java主要语句的格式；掌握异常处理的概念以及try/catch/finally/thow/throws的使用</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="Java程序的结构"><a href="#Java程序的结构" class="headerlink" title="Java程序的结构"></a>Java程序的结构</h2><ul>
<li>一个Java程序由一个或多个<code>.java</code>源文件组成。每个源文件中包含一个或多个类/接口，但只能有一个public类型的类，源文件名要与这个公有类名称一致。</li>
<li>一个Java程序的结构包含以下内容：<ul>
<li>package包语句，最多只有一个，须写在源文件开始的地方；</li>
<li>import引入语句，可有可无，若有则应写在所有类定义的前面；</li>
<li>public权限的类定义，最有只有一个；</li>
<li>类定义：个数无限制；</li>
<li>接口定义：个数没有限制；</li>
</ul>
</li>
<li>包是类的容器，用来划分命名空间，以免类名冲突。语法为：<code>packge pkg1[.pkg2[.pkg2...]]</code>。</li>
<li>package语句是源文件中，第一条非注释语句，一个源文件中最多只能有一条package语句。</li>
<li>包的名字有层次关系，各层之间以点分隔。报名全部用小写字母。</li>
<li>一个包可以包含若干个类文件，也可以包含若干个包。</li>
</ul>
<h2 id="流程控制"><a href="#流程控制" class="headerlink" title="流程控制"></a>流程控制</h2><ul>
<li>语句是Java最小执行单位，以分号(;)作为分隔符。</li>
<li>语句分为单语句和复合语句。单语句就是一条语句，复合语句是用大括号包含的语句组，也称为语句块。</li>
</ul>
<h3 id="1、赋值语句"><a href="#1、赋值语句" class="headerlink" title="1、赋值语句"></a>1、赋值语句</h3><ul>
<li>将一个值赋值给某个变量，称之为赋值语句。</li>
<li>值得注意的是，语句和表达式有很多相同点，也有不同的地方。表达式可以当作语句，但语句不一定都是表达式；表达式都会得到一个值，也就是表达式的计算结果，而语句也会有一个值，但不是计算结果，而是执行结果。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">costomer1 = <span class="keyword">new</span> Customer(); <span class="comment">// 赋值语句</span></span><br><span class="line">x = <span class="number">12</span>; <span class="comment">// 赋值语句</span></span><br><span class="line"></span><br><span class="line">x++; <span class="comment">// 表达式语句</span></span><br></pre></td></tr></table></figure>

<h3 id="2、分支语句"><a href="#2、分支语句" class="headerlink" title="2、分支语句"></a>2、分支语句</h3><p>分支语句指根据一定的条件，动态决定程序的执行方向。</p>
<h4 id="if语句"><a href="#if语句" class="headerlink" title="if语句"></a>if语句</h4><ul>
<li>if语句的条件表达式必须得到一个逻辑值</li>
<li>if语句的else子句是可选的</li>
<li>通过嵌套if语句可以实现多重选择，也就是多个分支的判断</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 形式1：无else子句</span></span><br><span class="line"><span class="keyword">if</span>(show)&#123;</span><br><span class="line">    Person.say(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 形式2：有else子句</span></span><br><span class="line"><span class="keyword">if</span>(!close)&#123;</span><br><span class="line">    Door.open()</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    Door.close()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 形式3：嵌套的if语句</span></span><br><span class="line"><span class="keyword">if</span>(yellowColor)&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;黄灯&quot;</span>)</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span>(redColor)&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;红灯&quot;</span>)</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span>(greenColor)&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;绿灯&quot;</span>)</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;其他&quot;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="switch语句"><a href="#switch语句" class="headerlink" title="switch语句"></a>switch语句</h4><ul>
<li>当需要进行多个条件的判断时，可以使用switch语句来实现，格式上更加简洁</li>
<li>switch语句的表达式计算结果必须是int型或者是char型，default子句是可选的</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">switchch(colorNum)&#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">        setBackground(Color.red);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>: </span><br><span class="line">        setBackground(Color.yellow);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">2</span>: </span><br><span class="line">        setBackground(Color.green);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        setBackground(Color.black);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3、循环语句"><a href="#3、循环语句" class="headerlink" title="3、循环语句"></a>3、循环语句</h3><h4 id="for语句"><a href="#for语句" class="headerlink" title="for语句"></a>for语句</h4><p>语法格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (初始语句; 条件表达式; 迭代语句)&#123;</span><br><span class="line">    循环体语句</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>初始语句和迭代语句可以包含多个语句，语句之间以逗号分隔。</li>
<li>先执行初始语句，判断条件表达式的值，如果为true，执行循环体语句，然后再执行迭代语句。下一个迭代再判断条件表达式的值，执行迭代语句，直到条件表达式的值为false，循环结束。</li>
<li>如果条件表达式的值一直为true，则循环会无限制的执行下去。</li>
</ul>
<h4 id="while语句"><a href="#while语句" class="headerlink" title="while语句"></a>while语句</h4><p>语法格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(条件表达式)&#123;</span><br><span class="line">    循环体语句</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>跟if语句一样，while语句的条件表达式需要返回布尔值，而不能是数值</li>
<li>while语句先计算条件表达式的值，为true时，重复执行循环体语句，直到条件按表达式值为false。</li>
</ul>
<h4 id="do-while语句"><a href="#do-while语句" class="headerlink" title="do-while语句"></a>do-while语句</h4><p>语法格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line">    循环体语句</span><br><span class="line">&#125; <span class="keyword">while</span>(条件表达式)</span><br></pre></td></tr></table></figure>

<ul>
<li>do语句先执行循环体语句，然后判断条件表达式是否为true，再重复执行循环，直到条件按表达式值为false则退出。</li>
</ul>
<h3 id="4、跳转语句"><a href="#4、跳转语句" class="headerlink" title="4、跳转语句"></a>4、跳转语句</h3><p>跳转语句由标号和语句组成<code>标号: 语句;</code></p>
<h4 id="break语句和container语句"><a href="#break语句和container语句" class="headerlink" title="break语句和container语句"></a>break语句和container语句</h4><ul>
<li>break语句可用于3类语句中：1. switch语句；2. for/while/do-while循环语句；3. 语句块；</li>
<li>break的语义是跳过本语句块的余下语句，执行到语句块末尾。</li>
<li>如果在循环语句中执行break，则是结束循环。</li>
<li>container用于循环语句，表示结束当次循环，进行下一次循环</li>
</ul>
<h2 id="输入-输出"><a href="#输入-输出" class="headerlink" title="输入/输出"></a>输入/输出</h2><h3 id="Scanner类"><a href="#Scanner类" class="headerlink" title="Scanner类"></a>Scanner类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Echo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        String message;</span><br><span class="line">        Scanner scan = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;Enter a line of text:&quot;</span>);</span><br><span class="line">        message = scan.nextLine();</span><br><span class="line">        System.out.println(<span class="string">&quot;You entered: &quot;</span> + message + <span class="string">&quot;\&quot;)</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="NumberFormat类和DecimalFormat类"><a href="#NumberFormat类和DecimalFormat类" class="headerlink" title="NumberFormat类和DecimalFormat类"></a>NumberFormat类和DecimalFormat类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"><span class="keyword">import</span> java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IntDouble</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="keyword">int</span> age;</span><br><span class="line">        <span class="keyword">double</span> weight, height, bmi;</span><br><span class="line">        Scanner scan = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入年龄:&quot;</span>);</span><br><span class="line">        age = scan.nextInt();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入体重(公斤):&quot;</span>);</span><br><span class="line">        weight = scan.nextDouble();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入身高(米):&quot;</span>);</span><br><span class="line">        height = scan.nextDouble();</span><br><span class="line"></span><br><span class="line">        bmi = weight / (height * height);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 保留三位小数</span></span><br><span class="line">        DecimalFormat fmt = <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;0.###&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;BMI:&quot;</span> + fmt.format(bmi));</span><br><span class="line">        fmt = <span class="keyword">new</span> DecimalFomat(<span class="string">&quot;0.##&quot;</span>);</span><br><span class="line">        <span class="comment">// 保留两位小数</span></span><br><span class="line">        System.out.println(<span class="string">&quot;BMI:&quot;</span> + fmt.format(bmi));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="处理异常"><a href="#处理异常" class="headerlink" title="处理异常"></a>处理异常</h2><ul>
<li>Java语言把错误分为两类：<ul>
<li>非致命性的错误，通过修正后，程序还能继续执行的（称为异常Exception）；</li>
<li>致命性的错误，比如程序内存耗尽，无需程序员处理的；</li>
</ul>
</li>
</ul>
<h3 id="异常及处理机制"><a href="#异常及处理机制" class="headerlink" title="异常及处理机制"></a>异常及处理机制</h3><ul>
<li>Java的异常处理机制预定义了Exception类，定义了程序产生异常的条件。也允许程序员自己编写特殊的异常处理程序，使得程序发生异常时，不是简单的结束程序，而是执行某段特殊的代码来处理这个异常，设法恢复程序的运行</li>
<li>如果发生了异常，程序会产生要给异常事件，相应地生成异常对象。这个对象包含了异常事件的类型，以及发生程序的运行状态。</li>
</ul>
<h3 id="异常分类"><a href="#异常分类" class="headerlink" title="异常分类"></a>异常分类</h3><ul>
<li>Exception类是所有异常类的父类，Error类是错误类的父类，它们都继承自Throwable类。</li>
<li>异常分为以下3种：<ul>
<li>受检异常（Checked Exception）：程序执行期间发生的严重事件，比如程序要用户输入地址读取某个文件，执行时却找不到该文件FileNotFoundException。编写程序的时候应该预见到这类错误，应该允许可以重新输入地址，从而恢复程序的执行。</li>
<li>运行时异常（Runtime Exception）：是程序中逻辑错误的结果，比如数组下标越界ArrayIndexOutOfBounds、除0错误ArithmneticException。该类错误都是RuntimeException的子类。</li>
<li>错误（Error）：比如内存溢出等情况，一般不需要处理；</li>
</ul>
</li>
</ul>
<h3 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h3><ul>
<li>异常要捕获之后，有两种处理的选择：在方法内处理异常，或者抛出让方法的调用者处理</li>
<li>使用try/catch来进行异常处理，try语句块包含可能捕获异常的代码段，catch语句块处理异常</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    anyObject.readString()</span><br><span class="line">&#125;<span class="keyword">catch</span>(IOException e)&#123;</span><br><span class="line">    <span class="comment">// 参数e，表示一个实际的异常</span></span><br><span class="line">    System.out.println(e.getMessage())</span><br><span class="line">&#125;<span class="keyword">catch</span>(FileNotFoundException e)&#123;</span><br><span class="line">    <span class="comment">// 可抛出不同类型的异常</span></span><br><span class="line">&#125;<span class="keyword">finally</span>&#123;</span><br><span class="line">    <span class="comment">// 总会执行的代码</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>如果一个方法引发了异常，但自己又不处理，则可通过thow语句进行抛出。值得注意的是，如果要抛出异常，方法定义时，要添加一个throws子句进行声明。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>William Leung
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/lianghuilin/murmur.git/university_notes/Java(3)-%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/" title="计应本的自考笔记(3)-Java程序的流程控制">https://gitee.com/lianghuilin/murmur.git/university_notes/Java(3)-程序的流程控制/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/murmur/university_notes/Java(2)-%E6%95%B0%E6%8D%AE%E4%B8%8E%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="prev" title="计应本的自考笔记(2)-Java数据与表达式">
                  <i class="fa fa-chevron-left"></i> 计应本的自考笔记(2)-Java数据与表达式
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/murmur/university_notes/Java(4)-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1/" rel="next" title="计应本的自考笔记(4)-Java面向对象程序设计">
                  计应本的自考笔记(4)-Java面向对象程序设计 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






      

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">
      

      

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">William Leung</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">142k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">2:09</span>
  </span>
</div>
<div class="busuanzi-count">
    <span id="busuanzi_container_site_uv">
      本站访客数<span id="busuanzi_value_site_uv"></span>人次
    </span>
    <span id="busuanzi_container_site_pv">
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/murmur/js/utils.js"></script><script src="/murmur/js/motion.js"></script><script src="/murmur/js/next-boot.js"></script>

  
















  <script>
    NProgress.configure({
      showSpinner: true
    });
    NProgress.start();
    document.addEventListener('readystatechange', () => {
      if (document.readyState === 'interactive') {
        NProgress.inc(0.8);
      }
      if (document.readyState === 'complete') {
        NProgress.done();
      }
    });
    document.addEventListener('pjax:send', () => {
      NProgress.start();
    });
    document.addEventListener('pjax:success', () => {
      NProgress.done();
    });
  </script>


  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>








  

  

</body>
</html>
